<?php

class __mysql extends __basic_def{
	
	protected $_linkID;
	protected $querySql;		// 最后一次查询的sql
	protected $numRows;	// 最后一次查询返回的行数
	
	function __construct(){
		global $config;
		
//		if (! extension_loaded ( 'mysql' )) {
//			halt ( "Not suppert mysql!" );
//		}
		
		$dsn = "mysql:host=".$config['db']['host'].";port=".$config['db']['port'].";dbname=". $config['db']['dbname'];
		if ($config['db']['pconnect']) {		// 长连接
			$this->_linkID = new PDO($dsn, $config['db']['user'], $config['db']['pass'], array( PDO::ATTR_PERSISTENT => true));
		}else{
			$this->_linkID = new PDO($dsn, $config['db']['user'], $config['db']['pass'], array( PDO::ATTR_PERSISTENT => false));
		}
		if (! $this->_linkID) {
			halt ( mysql_error () );
		}
		
		// 设置 sql_model
		$this->_linkID->exec ("
				SET CHARACTER_SET_CLIENT = " . $config["db"]["dbcharset"] . ",
				CHARACTER_SET_CONNECTION = " . $config["db"]["dbcharset"] . ",
				CHARACTER_SET_DATABASE = " . $config["db"]["dbcharset"] . ",
				CHARACTER_SET_RESULTS = " . $config["db"]["dbcharset"] . ",
				CHARACTER_SET_SERVER = " . $config["db"]["dbcharset"] . ",
				COLLATION_CONNECTION = " . $config["db"]["dbcharset"] . "_general_ci,
				COLLATION_DATABASE = " . $config["db"]["dbcharset"] . "_general_ci,
				COLLATION_SERVER = " . $config["db"]["dbcharset"] . "_general_ci,
				AUTOCOMMIT=0" );
	}
	
	function __destruct(){}
	
	/**
	 * 执行sql并返回数据集
	 * @param string $sql
	 * @return boolean|PDOStatement
	 */
	public function query($sql){
		if (!$this->_linkID)	return false;
		$this->querySql = $sql;
		$result = $this->_linkID->query($sql);
		if(false === $result) halt($this->_linkID->errorInfo());
		return $result;
	}
	
	public function insert_id(){
		if (!$this->_linkID)	return false;
		return $this->_linkID->lastInsertId();
	}
	
	/**
	 * 执行语句
	 *
	 * @access public
	 * @param string $sql sql指令
	 * @return 返回执行状态
	 */
	public function execute($sql) {
		if (! $this->_linkID) return false;
		$this->queryStr = $sql;
		$result = $this->_linkID->exec($sql);
		return $result;
	}
	
	/**
	 * 获取单行数据
	 * @param string $sql
	 * @return multitype:
	 */
	public function get_row($sql){
		$rs = $this->query($sql);
		if(false === $rs) halt("fail");
		$rs->setFetchMode(PDO::FETCH_ASSOC);
		$row = $rs->fetch();
		return $row;
	}
	
	
	/**
	 * 获取多行数据
	 * @param string $sql
	 * @return multitype:
	 */
	public function get_rows($sql){
		$rs = $this->query($sql);
		if(false === $rs) halt("fail");
		$rs->setFetchMode(PDO::FETCH_ASSOC);
		$result = $rs->fetchAll();
		return $result;
	}
	
	/**
	 * 启动事务
	 *
	 * @access public
	 * @return boolean
	 */
	public function startTrans() {
		if (! $this->_linkID) return false;
		$this->_linkID->beginTransaction();
		return true;
	}
	/**
	 * 用于非自动提交状态下面的查询提交
	 *
	 * @access public
	 * @return boolean
	 */
	public function commit() {
		if (! $this->_linkID) return false;
		$this->_linkID->commit();
		return true;
	}
	/**
	 * 事务回滚
	 *
	 * @access public
	 * @return boolen
	 */
	public function rollback() {
		if (! $this->_linkID) return false;
		$this->rollback();
		return true;
	}
	
	
	
	
}
